﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace StargateWarsClient
{
    public partial class Form1 : Form
    {
        private delegate void SetProgressValues();
        private bool JuegoActivo = true;

        private int MaxMineral = 10000;
        private int MaxEnergia = 10000;
        private int MaxCasco = 10000;
        private int MaxEscudo = 10000;

        //Mineral en el hangar
        private float Mineral = 10000;
        //Cantidad de Energía
        private float Energia =1000;
        //Estado del Generador
        private bool GeneradorActivo = true;
        //Factor de Conversion de Mineral a Energía en %
        private float FactorEnergia = 50;
        //Ancho de Banda de la nave (unidades de mineral que la nave procesa por ciclo)
        private float AnchoDeBanda = 100;
        //Cantidad de Casco
        private float Casco = 10000;
        //Cantidad de Escudo
        private float Escudo = 10000;
        //Estado del Escudo
        private bool EscudoActivo = true;

        public Form1()
        {
            InitializeComponent();
            Thread thTimeLine = new Thread(MainTimeLine);

            //Carga de Recursos
            pgEnergia.Maximum = MaxEnergia;
            pgNivelCasco.Maximum = MaxCasco;
            pgNivelEscudo.Maximum = MaxEscudo;
            pgNivelMineral.Maximum = MaxMineral;
            //Iniciar el bucle principal del juego
            thTimeLine.Start();
        }

        private void ActualizarGenerador()
        {
            if (GeneradorActivo)
            {
                if (Mineral >= AnchoDeBanda)
                {
                    Energia = Energia + (AnchoDeBanda * (FactorEnergia / 100));
                    Mineral = Mineral - AnchoDeBanda;
                }
                else
                {
                    AnchoDeBanda = Mineral;
                    Energia = Energia + (AnchoDeBanda * (FactorEnergia / 100));
                    Mineral = Mineral - AnchoDeBanda;
                }
                setProgress();
            }
        }

        private void MainTimeLine()
        {
            do
            {
                //Función de bucle continuo
                ActualizarGenerador();
                Thread.Sleep(1000);
            } while (JuegoActivo);
        }

        private void setProgress()
        {
            if (pgEnergia.InvokeRequired)
            {
                SetProgressValues pv = new SetProgressValues(setProgress);
                this.Invoke(pv);
            }
            else
            {
                pgEnergia.Value = (int)Energia;
                if (pgEnergia.Value < (MaxEnergia / 1)) pgEnergia.ForeColor = Color.Green;
                if (pgEnergia.Value < (MaxEnergia / 2)) pgEnergia.ForeColor = Color.YellowGreen;
                if (pgEnergia.Value < (MaxEnergia / 3)) pgEnergia.ForeColor = Color.Yellow;
                if (pgEnergia.Value < (MaxEnergia / 4)) pgEnergia.ForeColor = Color.Red;
            }

            if (pgNivelMineral.InvokeRequired)
            {
                SetProgressValues pv = new SetProgressValues(setProgress);
                this.Invoke(pv);
            }
            else
            {
                pgNivelMineral.Value = (int)Mineral;
                if (pgNivelMineral.Value < (MaxMineral / 1.75f)) pgNivelMineral.ForeColor = Color.Green;
                if (pgNivelMineral.Value < (MaxMineral / 2)) pgNivelMineral.ForeColor = Color.YellowGreen;
                if (pgNivelMineral.Value < (MaxMineral / 3)) pgNivelMineral.ForeColor = Color.Yellow;
                if (pgNivelMineral.Value < (MaxMineral / 4)) pgNivelMineral.ForeColor = Color.Red;
            }
        }

        private void trackGenerador_Scroll(object sender, EventArgs e)
        {
            //Regula el consumo de mineral
            AnchoDeBanda = trackGenerador.Value;
            lblCantGenerador.Text = trackGenerador.Value.ToString()+"%";
        }

        void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            JuegoActivo = false;
        }

}
    }
